<template>
  <el-dialog
    :title="!dataForm.ticketId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="展览编号" prop="exhibitionNo">
      <el-input v-model="dataForm.exhibitionNo" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="展览名字" prop="exhibitionName">
      <el-input v-model="dataForm.exhibitionName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="展览地址" prop="exhibitionAddress">
      <el-input v-model="dataForm.exhibitionAddress" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="展览开始时间" prop="exhibitionBeginTime">
      <el-input v-model="dataForm.exhibitionBeginTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="展览结束时间" prop="exhibitionEndTime">
      <el-input v-model="dataForm.exhibitionEndTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="门票总数量" prop="remainQuantity">
      <el-input v-model="dataForm.remainQuantity" placeholder=""></el-input>
    </el-form-item>
    <!-- <el-form-item label="" prop="visitorIdCard">
      <el-input v-model="dataForm.visitorIdCard" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="visitorName">
      <el-input v-model="dataForm.visitorName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getTicketById, saveOrUpdateTicket } from '@/api/tickets'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        ticketId: 0,
        exhibitionNo: '',
        exhibitionName: '',
        exhibitionAddress: '',
        exhibitionBeginTime: '',
        exhibitionEndTime: '',
        remainQuantity: '',
        visitorIdCard: '',
        visitorName: '',
        createTime: '',
        updateTime: ''
      },
      dataRule: {
        exhibitionNo: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        exhibitionName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        exhibitionAddress: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        exhibitionBeginTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        exhibitionEndTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        remainQuantity: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        visitorIdCard: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        visitorName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.ticketId = id || 0
      this.visible = true
      this.$nextTick(async () => {
        this.$refs.dataForm.resetFields()
        if (this.dataForm.ticketId) {
          const { data } = await getTicketById(this.dataForm.ticketId)
          if (data && data.code === 0) {
            this.dataForm.exhibitionNo = data.tickets.exhibitionNo
            this.dataForm.exhibitionName = data.tickets.exhibitionName
            this.dataForm.exhibitionAddress = data.tickets.exhibitionAddress
            this.dataForm.exhibitionBeginTime = data.tickets.exhibitionBeginTime
            this.dataForm.exhibitionEndTime = data.tickets.exhibitionEndTime
            this.dataForm.remainQuantity = data.tickets.remainQuantity
            this.dataForm.visitorIdCard = data.tickets.visitorIdCard
            this.dataForm.visitorName = data.tickets.visitorName
            this.dataForm.createTime = data.tickets.createTime
            this.dataForm.updateTime = data.tickets.updateTime
          }

          // this.$http({
          //   url: `/tickets/info/${this.dataForm.ticketId}`,
          //   method: 'get'
          // }).then(({ data }) => {
          //   if (data && data.code === 0) {
          //     this.dataForm.exhibitionNo = data.tickets.exhibitionNo
          //     this.dataForm.exhibitionName = data.tickets.exhibitionName
          //     this.dataForm.exhibitionAddress = data.tickets.exhibitionAddress
          //     this.dataForm.exhibitionBeginTime = data.tickets.exhibitionBeginTime
          //     this.dataForm.exhibitionEndTime = data.tickets.exhibitionEndTime
          //     this.dataForm.remainQuantity = data.tickets.remainQuantity
          //     this.dataForm.visitorIdCard = data.tickets.visitorIdCard
          //     this.dataForm.visitorName = data.tickets.visitorName
          //     this.dataForm.createTime = data.tickets.createTime
          //     this.dataForm.updateTime = data.tickets.updateTime
          //   }
          // })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs.dataForm.validate(async (valid) => {
        if (valid) {
          const ticket = {
            ticketId: this.dataForm.ticketId || undefined,
            exhibitionNo: this.dataForm.exhibitionNo,
            exhibitionName: this.dataForm.exhibitionName,
            exhibitionAddress: this.dataForm.exhibitionAddress,
            exhibitionBeginTime: this.dataForm.exhibitionBeginTime,
            exhibitionEndTime: this.dataForm.exhibitionEndTime,
            remainQuantity: this.dataForm.remainQuantity
            // visitorIdCard: this.dataForm.visitorIdCard,
            // visitorName: this.dataForm.visitorName,
            // createTime: this.dataForm.createTime,
            // updateTime: this.dataForm.updateTime
          }
          const { data } = await saveOrUpdateTicket(this.dataForm.ticketId, ticket)
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          } else {
            this.$message.error(data.msg)
          }

          // this.$http({
          //   url: `/tickets/${!this.dataForm.ticketId ? 'save' : 'update'}`,
          //   method: 'post',
          //   data: {
          //     ticketId: this.dataForm.ticketId || undefined,
          //     exhibitionNo: this.dataForm.exhibitionNo,
          //     exhibitionName: this.dataForm.exhibitionName,
          //     exhibitionAddress: this.dataForm.exhibitionAddress,
          //     exhibitionBeginTime: this.dataForm.exhibitionBeginTime,
          //     exhibitionEndTime: this.dataForm.exhibitionEndTime,
          //     remainQuantity: this.dataForm.remainQuantity
          //     // visitorIdCard: this.dataForm.visitorIdCard,
          //     // visitorName: this.dataForm.visitorName,
          //     // createTime: this.dataForm.createTime,
          //     // updateTime: this.dataForm.updateTime
          //   }
          // }).then(({ data }) => {
          //   if (data && data.code === 0) {
          //     this.$message({
          //       message: '操作成功',
          //       type: 'success',
          //       duration: 1500,
          //       onClose: () => {
          //         this.visible = false
          //         this.$emit('refreshDataList')
          //       }
          //     })
          //   } else {
          //     this.$message.error(data.msg)
          //   }
          // })
        }
      })
    }
  }
}
</script>
